/*******************************************************************************
 * Package: com.hadoop.mr
 * Type:    WordCountReduce
 * Date:    2024-05-14 22:35
 *
 * Copyright (c) 2024 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.yarn.flow;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 功能描述： 聚合
 * Reducer1: <手机号12,<FlowWritable,FlowWritable,FlowWritable,FlowWritable>>
 * Reducer2: <手机号13,<FlowWritable,FlowWritable,FlowWritable>>
 * Reducer3: <手机号14,<FlowWritable>>
 *
 * @author Songxianyang
 * @date 2024-05-14 22:35
 */
public class FlowReduce extends Reducer<Text, FlowWritable, Text, FlowWritable> {
    @Override
    protected void reduce(Text key, Iterable<FlowWritable> values, Context context) throws IOException, InterruptedException {
        // 循环map 给过来的这个<手机号12,<FlowWritable,FlowWritable,FlowWritable,FlowWritable>>结构
        // 定义变量
        Long up = 0L;
        Long down = 0L;
        for (FlowWritable value : values) {
            up = value.getUp() + up;
            down = value.getDown() + down;
        }
        // 写入上下文 求和
        context.write(key, new FlowWritable(up,down));

        System.out.println("---------Reduce--------");
        System.out.println(key + ":Reduce:" + new FlowWritable(up,down));
    }
}
